package com.dt.app.api.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dt.app.modules.product.entities.Product;
import com.dt.app.modules.product.vo.ProductBean;
import com.dt.app.api.product.mapper.ProductMapper;
import com.dt.app.api.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鸿源塔基
 * @since 2024-03-07
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    public List<Product> getProducts(Integer categoryId){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.orderByDesc("seq");
        return this.list(wrapper);
    }
    public List<ProductBean> getProductBeans(Integer categoryId){
        List<Product> products = getProducts(categoryId);
        if(products==null || products.size()<1) return null;
        List<ProductBean> beans=new ArrayList<>();
        products.forEach(it -> {
            ProductBean bean = new ProductBean();
            BeanUtils.copyProperties(it,bean);
            beans.add(bean);
        });
        return beans;
    }
}
